package gen

import "github.com/lab-online/pkg/logger"

func MergeCodeMap(maps ...CodeMap) CodeMap {
	modSet := make(map[string]bool)

	result := make(CodeMap)
	for _, item := range maps {
		for modKey, mod := range item {
			if _, ok := modSet[modKey]; ok {
				copyMap(result, modKey, mod)
			} else {
				modSet[modKey] = true
				result[modKey] = mod
			}
		}
	}
	return result
}

func copyMap(target CodeMap, modKey string, mod map[int]string) {
	for code, desc := range mod {
		if _, ok := target[modKey][code]; ok {
			logger.Error("code already exists", modKey, code, desc)
			panic("code already exists")
		}

		target[modKey][code] = desc
	}
}
